package capitulo5;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.net.URL;
import java.util.Random;

import javax.swing.JFrame;

// Random Images program
@SuppressWarnings("serial")
public class RandomImages extends JFrame {
	private Image image;
	private Image image2;
	
	public static void main(String[] args) {
		new RandomImages();
	}
	
	// Constructor
	public RandomImages() {
		super("RandomImages");
		setSize(600,500);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Toolkit tk = Toolkit.getDefaultToolkit();
		image = tk.getImage(getURL("spaceship.png"));	
		image2 = tk.getImage(getURL("asteroid2.png"));
	}
	
	// Affine transformation
	AffineTransform identity = new AffineTransform();
	
	private URL getURL(String filename){
		URL url = null;
		try {
			url = this.getClass().getResource(filename);
		} catch(Exception e) {}
		return url;
	} 

	// Paint Event
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		
		// Working with transform object
		AffineTransform trans = new AffineTransform();
		
		// Random name generator
		Random rand = new Random();
		
		// applet window width/height
		int width = getSize().width;
		int height = getSize().height;
		
		// fill the background with BLACK
		g2d.setColor(Color.BLACK);
		g2d.fillRect(0, 0, width, height);
		
		// draw the image multiple times
		/*for (int n=0; n<50; n++){
			trans.setTransform(identity);
			// Move, rotate, scale the image randomly
			int nrand1 = rand.nextInt();
			int nrand2 = rand.nextInt();
			int div1 = nrand1 % width; 
			int div2 = nrand2 % height;
			trans.translate(div1, div2);
			trans.rotate(Math.toRadians(360*rand.nextDouble()));
			double scale = rand.nextDouble()+1;
			trans.scale(scale, scale);*/
		
			trans.setTransform(identity);
			trans.translate(300, 200);
			trans.rotate(Math.toRadians(180));
			
			//Draw the image
			
			g2d.drawImage(image, trans, this);
			
			// Exercice 1
			
			trans.setTransform(identity);
			trans.translate(rand.nextInt() % width, rand.nextInt() % height);
			g2d.drawImage(image2, trans, this);
			
		//}
	}
	
}
